package com.javasm.duanju.controller;
import com.javasm.duanju.vo.VideoInfoVO;
import utils.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javasm.duanju.dto.VideoBaseDTO;
import com.javasm.duanju.entity.DraftVideo;
import com.javasm.duanju.service.DraftVideoService;
import com.javasm.duanju.vo.DataPageVO;
import com.javasm.duanju.vo.VideoVo;
import commom.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.BeanUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (DraftVideo)表控制层
 *
 * @author makejava
 * @since 2025-02-26 19:29:26
 */
@RestController
@RequestMapping("/draftVideo")
@Api(value = "draftVideo", tags = "短剧基本信息")
public class DraftVideoController{

  @Autowired
    private DraftVideoService draftVideoService;


    @PostMapping("/add")
  @ApiOperation(value = "短剧添加", notes = "短剧添加")
  public AjaxResult addVideo(@RequestBody VideoBaseDTO videoBaseDTO){

      return AjaxResult.ok(draftVideoService.addVideo(videoBaseDTO));
  }

    @PostMapping("/update")
    @ApiOperation(value = "短剧修改", notes = "短剧修改")
    public AjaxResult updateVideo(@RequestBody VideoBaseDTO videoBaseDTO){
        return AjaxResult.ok(draftVideoService.updateVideo(videoBaseDTO));
    }
     @ApiOperation(value = "上架短剧",notes = "上架短剧")
     @PostMapping("/upvideo")
     public AjaxResult UpVideo(@RequestBody VideoBaseDTO videoBaseDTO){
      draftVideoService.UpVideo(videoBaseDTO);
       return AjaxResult.ok();
     }

    @ApiOperation(value = "导入短剧",notes = "导入短剧")
    @GetMapping("/import")
    public AjaxResult importVideo(MultipartFile file) throws IOException {
        draftVideoService.importVideo(file);
        return AjaxResult.ok();
    }

    @ApiOperation(value = "导出短剧",notes = "导出短剧")
    @GetMapping("/export")
    public void exportVideo(HttpServletResponse response) throws IOException {
        draftVideoService.exportVideo(response);
    }
    @ApiOperation(value = "删除指定的短剧",notes = "删除指定的短剧")
    @GetMapping("/videoDel")
    public AjaxResult videoDel(@RequestBody VideoBaseDTO videoBaseDTO)  {
        draftVideoService.videoDel(videoBaseDTO);
        return AjaxResult.ok();
    }
//    短剧分页查询
@ApiOperation(value = "分页查询",notes = "分页查询")
@GetMapping("/page")
public AjaxResult getVideoPage(Page<DraftVideo> page,DraftVideo draftVideo) {
    // 1. 查询原始分页数据
    Page<DraftVideo> draftVideoPage = draftVideoService.page(page,new QueryWrapper<>(draftVideo));

    // 2. 转换为 VideoVo 列表
    List<VideoVo> videoVoList = draftVideoPage.getRecords().stream()
            .map(draftVideo1 -> {
                VideoVo videoVo = new VideoVo();
                videoVo.setId(draftVideo1.getId());
                videoVo.setName(draftVideo1.getName());
                videoVo.setCoverUrl(draftVideo1.getCoverUrl());
                videoVo.setTags(draftVideo1.getTags());
                videoVo.setTotalCount(draftVideo1.getTotalCount());
                videoVo.setFollowCount(draftVideo1.getFollowCount());
                videoVo.setActor(draftVideo1.getActor()); // 修复重复设置
                return videoVo;
            })
            .collect(Collectors.toList());

    // 3. 构建新的 Page<VideoVo> 对象（关键！）
    Page<VideoVo> videoVoPage = new Page<>(
            draftVideoPage.getCurrent(),
            draftVideoPage.getSize(),
            draftVideoPage.getTotal()
    );
    videoVoPage.setRecords(videoVoList);
    videoVoPage.setPages(draftVideoPage.getPages());
    // 4. 封装到 DataPageVO<VideoVo> 中
    DataPageVO<VideoVo> dataPageVO = new DataPageVO<>(videoVoPage);
    return AjaxResult.ok(dataPageVO);
}
    @ApiOperation(value = "短剧详情接口",notes = "短剧详情接口")
    @GetMapping("/selectByVideo/{id}")
    public AjaxResult selectByVideo(@PathVariable Long id)  {
   VideoInfoVO videoInfoVO= draftVideoService.videoInfoById(id);
        return AjaxResult.ok(videoInfoVO);
    }
    @ApiOperation(value = "最新上架的短剧",notes = "最新上架的短剧")
    @GetMapping("/selectSortVideo")
    public AjaxResult selectSortVideo(@RequestBody VideoBaseDTO videoBaseDTO)  {
//        VideoInfoVO videoInfoVO= draftVideoService.videoInfoById(id);
//        TODO:根据时间排序前20条
        return AjaxResult.ok();
    }
    @ApiOperation(value = "短剧播放页面信息",notes = "短剧播放页面信息")
    @GetMapping("/selectOpenVideo")
    public AjaxResult selectOpenVideo(@RequestBody VideoBaseDTO videoBaseDTO)  {
        VideoInfoVO videoInfoVO= draftVideoService.selectOpenVideo(videoBaseDTO);
        return AjaxResult.ok(videoInfoVO);
    }




}

